import { SlotType, DefaultItemMargin, SelectType, DefaultOpProp, initFormModel } from "../utils/index.js";
import { defineComponent, ref, reactive, onBeforeUpdate } from "vue";
import FormItem from "./UtFormItem.js";
import { ElForm } from "../node_modules/element-plus/es/components/form/index.js";
function getOnChangeListSet(option) {
  const map = {};
  for (let i = 0; i < option.list.length; i++) {
    const item = option.list[i];
    const { onChangeLis } = item;
    if (onChangeLis) {
      for (let j = 0; j < onChangeLis.key.length; j++) {
        const key = onChangeLis.key[j];
        let list = map[key];
        if (!list) {
          list = [];
          map[key] = list;
        }
        list.push(item);
      }
    }
  }
  return map;
}
async function onDataChange(onChangeListMap, keys, model) {
  if (!keys || keys.length == 0) {
    return;
  }
  const changeMap = {};
  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];
    const list = onChangeListMap[key];
    if (list) {
      for (let j = 0; j < list.length; j++) {
        const item = list[j];
        let obj = changeMap[item.name];
        if (!obj) {
          obj = {
            changeKeys: [],
            item
          };
          changeMap[item.name] = obj;
        }
        obj.changeKeys.push(key);
      }
    }
  }
  const keysMap = {};
  for (const key in changeMap) {
    const { item, changeKeys } = changeMap[key];
    const change = await item.onChangeLis.func(model, item, changeKeys);
    if (change) {
      keysMap[change] = "";
    }
  }
  await onDataChange(onChangeListMap, Object.keys(keysMap), model);
}
function initFormSlot(option, model, context, onchange) {
  if (option && option.list) {
    for (let i = 0; i < option.list.length; i++) {
      const item = option.list[i];
      if (item.inputType !== SlotType && item.slotFuc && typeof item.slotFuc === "function") {
        item.slots = item.slotFuc(option, item, model, context, onchange);
      }
    }
  }
}
async function initFormOption(option) {
  if (option && option.list) {
    option.itemMargin = option.itemMargin || DefaultItemMargin;
    for (let i = 0; i < option.list.length; i++) {
      const item = option.list[i];
      if (item.inputType === SelectType) {
        if (!item.optionProp) {
          item.optionProp = DefaultOpProp;
        }
      }
      if (item.optionFuc && typeof item.optionFuc === "function") {
        await item.optionFuc(item);
      }
    }
  }
  return option || {};
}
const FormGroup = defineComponent({
  name: "UtFormGroup",
  props: {
    option: {
      type: Object
    },
    model: {
      type: Object
    },
    disabled: Boolean
  },
  setup(props, context) {
    const { expose, slots } = context;
    const form = ref(null);
    const data = reactive({});
    const state = reactive(props.model);
    const validate = (callback) => {
      form.value.validate(callback);
    };
    onBeforeUpdate(() => {
    });
    const onchange = async (item) => {
      console.log(item);
      if (onChangeLis) {
        await onDataChange(onChangeLis, [item.name], state);
      }
    };
    let onChangeLis;
    initFormOption(props.option).then((op) => {
      for (const key in op) {
        data[key] = op[key];
      }
      initFormSlot(data, state, context, onchange);
      onChangeLis = getOnChangeListSet(data);
    });
    const reset = () => {
      const formData = initFormModel(props.option);
      for (const key in state) {
        delete state[key];
      }
      for (const key in formData) {
        state[key] = formData[key];
      }
    };
    expose({ validate, reset });
    return () => {
      return /* @__PURE__ */ React.createElement(ElForm, {
        ref: form,
        inline: true,
        ...data.props,
        model: state
      }, data.list && data.list.map((item) => {
        if (item.hidden) {
          return null;
        }
        if (item.inputType !== SlotType) {
          return /* @__PURE__ */ React.createElement(FormItem, {
            item,
            model: state,
            option: data,
            disabled: props.disabled || data.disabled,
            onChange: (item2) => {
              onchange(item2);
            },
            onValUpdate: (key, value) => {
              state[key] = value;
            }
          });
        } else {
          if (item.slotFuc) {
            return item.slotFuc(data, item, state, context, onchange);
          } else {
            const slot = slots ? slots[item.name] : null;
            if (slot) {
              const slotProps = {
                data,
                item,
                model: state,
                context,
                onchange
              };
              return slot(slotProps);
            } else {
              return null;
            }
          }
        }
      }), slots && slots.default && slots.default());
    };
  }
});
export {
  FormGroup as default
};
